package cn.aiweiyi.qingjing.coupon.distribution.controller;

import cn.aiweiyi.qingjing.coupon.comm.exception.CouponException;
import cn.aiweiyi.qingjing.coupon.comm.vo.AcquireCouponRequest;
import cn.aiweiyi.qingjing.coupon.comm.vo.CouponTemplateSDK;
import cn.aiweiyi.qingjing.coupon.comm.vo.SettlementInfo;
import cn.aiweiyi.qingjing.coupon.distribution.entity.Coupon;
import cn.aiweiyi.qingjing.coupon.distribution.feign.TemplateClient;
import cn.aiweiyi.qingjing.coupon.distribution.service.UserCouponService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 青衿
 * @Date 2020/11/23 15:55
 * 用于响应客户端的优惠券业务请求
 * 调用UserCOuponService执行优惠券业务的控制器类
 **/
@Slf4j
@RestController
public class UserCouponController {
    @Autowired
    private UserCouponService userCouponService;

    /**
     * 根据用户和状态查找优惠券
     *
     * @param userId
     * @param status
     * @return
     * @throws CouponException
     */
    @GetMapping("/coupons")
    public List<Coupon> findCouponsByStatus(
            @RequestParam Long userId,
            @RequestParam Integer status) throws CouponException {
        log.info("为用户{}查找{}状态的优惠券", userId, status);
        return userCouponService.findCouponsByStatus(userId, status);
    }


    /**
     * 根据用户查找可领取的优惠券模板
     *
     * @param userId
     * @return
     * @throws CouponException
     */
    @GetMapping("/templates")
    public List<CouponTemplateSDK> findAvailableTemplate(
            @RequestParam Long userId) throws CouponException {
        log.info("为用户{}查找{}可领取的优惠券模板", userId);
        return userCouponService.findAvailableTemplate(userId);
    }


    /**
     * 用户优惠券
     *
     * @param req
     * @return
     * @throws CouponException
     */
    @PostMapping("/coupons/acquire")
    public Coupon findAvailableTemplate(
            @RequestBody AcquireCouponRequest req
    ) throws CouponException {
        System.out.println("进入了" + req);
        log.info("领取优惠券:{}", JSON.toJSONString(req));
        return userCouponService.acquireCoupon(req);
    }

    @PostMapping("/coupons/settlement")
    public SettlementInfo settlementInfo(@RequestBody SettlementInfo info) throws CouponException {
        log.info("结算优惠券:{}", JSON.toJSONString(info));
        return userCouponService.settlement(info);
    }

}
